package com.ljh.cloudmusic.service;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

/**
 * @author: Themberfue
 * @date: 2025/3/16 9:06
 * @description:
 */
@Service
public class MailService {
    @Autowired
    private JavaMailSender javaMailSender;

    // 发送邮件的方法
    public void sendEmail(String toEmail, String subject, String body) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);

        // 这里的 from 必须和 spring.mail.username 一致！
        messageHelper.setFrom("themberfue@qq.com");
        messageHelper.setTo(toEmail);
        messageHelper.setSubject(subject);
        messageHelper.setText(body);

        // 发送邮件
        javaMailSender.send(mimeMessage);
    }
}
